# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        foods_dict = {}
        table_food_dict = {}

        for item in orders:
            foods_dict[item[2]] = foods_dict.get(item[2], 0) + 1
            table_id = int(item[1])
            if table_food_dict.get(table_id, None) is None:
                table_order = {item[2]: 1}
                table_food_dict[table_id] = table_order
            else:
                table_order = table_food_dict.get(table_id)
                table_order[item[2]] = table_order.get(item[2], 0) + 1

        row = ["Table"]
        foods_list = sorted(foods_dict.keys())
        for food in foods_list:
            row.append(food)

        ans = [row]

        table_ids = sorted(table_food_dict.keys())
        for key in table_ids:
            row = [str(key)]
            table_order = table_food_dict[key]
            for food in foods_list:
                row.append(str(table_order.get(food, 0)))

            ans.append(row)

        return ans

    def check_result(self, expect, result) -> bool:
        return self.check_result_array(expect, result)


if __name__ == '__main__':
    solution = Solution()

    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        ([["David", "3", "Ceviche"], ["Corina", "10", "Beef Burrito"], ["David", "3", "Fried Chicken"],
          ["Carla", "5", "Water"], ["Carla", "5", "Ceviche"], ["Rous", "3", "Ceviche"]],
         [["Table", "Beef Burrito", "Ceviche", "Fried Chicken", "Water"], ["3", "0", "2", "1", "0"],
          ["5", "0", "1", "0", "1"], ["10", "1", "0", "0", "0"]]),
        ([["James", "12", "Fried Chicken"], ["Ratesh", "12", "Fried Chicken"], ["Amadeus", "12", "Fried Chicken"],
          ["Adam", "1", "Canadian Waffles"], ["Brianna", "1", "Canadian Waffles"]],
         [["Table", "Canadian Waffles", "Fried Chicken"], ["1", "2", "0"], ["12", "0", "3"]]
         ),
        (
            [["Laura", "2", "Bean Burrito"], ["Jhon", "2", "Beef Burrito"], ["Melissa", "2", "Soda"]],
            [["Table", "Bean Burrito", "Beef Burrito", "Soda"], ["2", "1", "1", "1"]]
        )
    ]

    for case_item in testcases:
        # 获取测试用的case和期待的执行结果
        input1 = case_item[0]
        expect = case_item[len(case_item) - 1]

        exec_result = solution.displayTable(input1)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

